package com.ultimatech.eurekaclient;

import com.ultimatech.cloud.poc.base.model.Message;
import com.ultimatech.cloud.poc.base.model.TestData;
import com.ultimatech.cloud.poc.base.model.UserVo;
import com.ultimatech.eurekaclient.model.User;
import com.ultimatech.eurekaclient.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;

/**
 * Created by zhangleping on 2017/9/27.
 */
@SpringBootApplication
@EnableEurekaClient
@RestController
public class ServiceHiApp {

    public static void main(String[] args) {
        SpringApplication.run(ServiceHiApp.class, args);
    }

    @Value("${server.port}")
    private String port;

    @Value("${eureka.instance.hostname}")
    private String hostname;

    @RequestMapping("/hi")
    public String home(@RequestParam String name) {
        return "hi " + name + ",i am from :" + hostname + ":" + port;
    }

    @Value("${spring.application.name}")
    private String serviceName;

    @RequestMapping("/whoami")
    public String whoami(@RequestParam String name) {
        return "Hi, " + name + " ! I'm Service " + this.serviceName + " on " + this.hostname;
    }

    @RequestMapping("/test")
    public TestData test(@RequestParam String name) {
        TestData testData = new TestData();
        testData.setId(1234);
        testData.setName(name);
        testData.setCreateData(new Date());
        return testData;
    }

    @Autowired
    private IUserService userService;

    @RequestMapping("/testmysql")
    public Message<UserVo> testMysql() {
        User user = new User();
        user.setName("test");
        user.setNumber("123456");
        Message<UserVo> message = userService.addUser(user, false);
        return message;
    }

    @RequestMapping(path = "/createUser")
    public Message<UserVo> createUser(@RequestBody UserVo user){
        User user1=new User();
        user1.setNumber(user.getNumber());
        user1.setName(user.getName());
        Message<UserVo> message=this.userService.addUser(user1,false);

        return message;
    }

    @RequestMapping(path = "/delUser")
    public Message<Integer> delUser(Integer userId){
        return this.userService.deleteUserById(userId);
    }

    @RequestMapping(path = "/updateUser")
    public Message<UserVo> updateUser(@RequestBody UserVo user){
        Message<UserVo> user1=this.userService.getById(user.getId());
        user1.getData().setName(user.getName());
        user1.getData().setNumber(user.getNumber());
        Message<UserVo> message=this.userService.updateUser(user1.getData());

        return message;
    }

    @RequestMapping(path = "/getuser")
    public Message<UserVo> getUserById(Integer id){
        Message<UserVo> user=this.userService.getById(id);
        return user;
    }

    @RequestMapping(path = "/findAllUser")
    public Message<List<UserVo>> findAllUser(){
        Message<List<UserVo>> list=this.userService.findAll();
        return list;
    }

}
